﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wallpaper.Models.Entities;

namespace Wallpaper.Data.Configurations
{
    public class UserConfiguration : IEntityTypeConfiguration<User>
    {
        public void Configure(EntityTypeBuilder<User> builder)
        {
            builder.ToTable("users");
            builder.HasKey(u => u.Id);

            builder.Property(u => u.Id)
                .HasMaxLength(36)
                .IsRequired()
                .HasComment("用户主键ID");

            builder.Property(u => u.OpenId)
                .HasMaxLength(100)
                .IsRequired(false)
                .HasComment("微信OpenId，用户唯一标识");

            builder.Property(u => u.Nickname)
                .HasMaxLength(36)
                .IsRequired(false)
                .HasComment("用户昵称");

            builder.Property(u => u.Signature)
                .HasMaxLength(200)
                .IsRequired(false)
                .HasComment("个性签名");

            builder.Property(u => u.Gender)
                .HasMaxLength(20)
                .IsRequired(false)
                .HasComment("性别：male-男,female-女,unknown-未知");

            builder.Property(u => u.AvatarUrl)
                .HasMaxLength(200)
                .IsRequired(false)
                .HasComment("头像图片URL地址");

            // 积分配置
            builder.Property(u => u.Points)
                .IsRequired()
                .HasDefaultValue(0)
                .HasComment("用户积分");

            // 免费下载次数配置
            builder.Property(u => u.TodayFreeDownloadCount)
                .IsRequired()
                .HasDefaultValue(3) // ✅ 默认每天3次免费
                .HasComment("今日剩余免费下载次数（每天重置）");

            // 最后下载日期配置
            builder.Property(u => u.LastDownloadDate)
                .IsRequired()
                .HasDefaultValueSql("CURRENT_TIMESTAMP") // ✅ DateTime用SQL默认值
                .HasComment("最后下载日期（用于判断是否跨天重置免费次数）");

            // IP地址配置
            builder.Property(u => u.IPAddress)
                .HasMaxLength(45) // IPv6最大长度45字符
                .IsRequired(false)
                .HasDefaultValue(null)
                .HasComment("用户IP地址");

            // 最后登录时间配置
            builder.Property(u => u.LastLoginTime)
                .IsRequired(false)
                .HasDefaultValue(null)
                .HasComment("最后登录时间");

            builder.Property(u => u.CreatedAt)
                .IsRequired()
                .HasDefaultValueSql("CURRENT_TIMESTAMP")
                .HasComment("账号创建时间");

            builder.Property(u => u.UpdatedAt)
                .IsRequired(false)
                .HasComment("最后更新时间，自动维护");

            builder.Property(u => u.IsEnabled)
                .HasDefaultValue(true)
                .HasComment("账号状态：true-启用,false-禁用");

            builder.Property(u => u.IsDeleted)
                .HasDefaultValue(false)
                .HasComment("软删除标志：true-已删除,false-正常");

            // 索引配置
            builder.HasIndex(u => u.OpenId)
                .IsUnique()
                .HasDatabaseName("idx_users_openid")
                .HasFilter("[OpenId] IS NOT NULL");

            builder.HasIndex(u => u.CreatedAt)
                .HasDatabaseName("idx_users_createdat");

            builder.HasIndex(u => u.LastLoginTime)
                .HasDatabaseName("idx_users_lastlogintime");

            builder.HasIndex(u => new { u.IsEnabled, u.IsDeleted })
                .HasDatabaseName("idx_users_status_deleted");

            builder.HasQueryFilter(u => u.IsDeleted == false);
        }

    }
}
